import api from '@api'
const state = {
  info: {
    data: {},
    shopId: ''
  },
  list: {
    data: [],
    shopId: ''
  },
  goods: {
    data: null,
    spec: [],
    goodsId: '',
    fruits: ''
  },
  eval: {
    list: [],
    pageNum: 0,
    page: 1,
    id: '',
    num: 0
  },
  modal: false
}

const getters = {
  ['getShopInfo'] (state) {
    return state.info.data
  },
  ['getShopList'] (state) {
    return state.list.data
  },
  ['specModal'] (state) {
    return state.modal
  },
  ['getGoods'] (state) {
    return state.goods
  },
  ['getEval'] (state) {
    return state.eval
  }
}

const actions = {
  async ['getShopInfo'] ({commit,state}, {shopId}) {

    if(shopId === state.info.shopId) {
      return
    }
    commit('clearGoods');
    const { data: { storeInfo, StatusCode} } = await api.get('api/v2/Store/storeInfo.ashx',{warehouseSn:shopId})
    if(StatusCode === '200'){
      commit('updateShopInfo', {info: storeInfo[0], shopId})
    }
  },
  async ['getShopList'] ({commit, state}, {shopId}) {
    if(shopId === state.list.shopId) {
      return
    }
    const { data: { goodList, StatusCode} } = await api.get('api/v2/store/storeGoodsList.ashx',{warehouseSn:shopId})
    if(StatusCode === '200'){
      commit('updateShopList', {goodList, shopId})
    }
  },
  async ['getGoods'] ({commit, state}, goodsId) {
    if(state.goods.goodsId == goodsId) {
      return
    }
    commit('updataGoods', {data: null, spec: [], fruits: '', goodsId})
    const { data: { goodsDetail, goodsSpecDetail, StatusCode, fruitList} } = await api.get('api/v2/Goods/goodsAllDetail.ashx',{goSn:goodsId})
    if(StatusCode === '200') {
      commit('updataGoods', {data:goodsDetail[0], spec: goodsSpecDetail, fruits:fruitList, goodsId})
    }
    return StatusCode
  },
  async ['getEval'] ({commit,state}, {id, page}) {
    if(state.eval.id == id) {
      return
    } else {
      commit('clearEvel');
    }
    let config = {
      warehouseSn: id,
      CurrentPage: page,
      PageSize:10
    }
    let {data: {evalNum, pageNum, StatusCode, orderDetail}} = await api.get('api/v2/Store/storeEval.ashx', config);
    if(StatusCode == '200') {
      commit('updataEval', {id:id , list:orderDetail , pageNum: parseInt(pageNum), page, num: evalNum})
    }

  },
  ['modal'] ({commit}, state) {
    commit('upModalState', state)
  }
}

const mutations = {
  ['updateShopInfo'] (state, {info, shopId }) {
    state.info.data = info;
    state.info.shopId = shopId
  },
  ['updateShopList'] (state, {goodList, shopId}) {
    state.list.data = goodList;
    state.list.shopId = shopId;
  },
  ['upModalState'] (state, modalState) {
    state.modal = modalState
  },
  ['updataGoods'] (state, {data, spec ,fruits, goodsId}) {
    state.goods.data = data;
    state.goods.spec = spec;
    state.goods.fruits = fruits;
    state.goods.goodsId = goodsId;
  },
  ['clearGoods'] (state) {
    state.goods={
      data: null,
      spec: [],
      goodsId: '',
      fruits: ''
    }
  },
  ['updataEval'] (state, {id, list, pageNum, page, num}) {
    state.eval.id = id;
    state.eval.list = state.eval.list.concat(list);
    state.eval.pageNum = pageNum;
    state.eval.page = page;
    state.eval.num = num;
  },
  ['clearEvel'] (state) {
    state.eval = {
      list: [],
      pageNum: 0,
      page: 1,
      id: '',
      num: 0
    }
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}